import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
import { createStyleImportPlugin, ElementPlusResolve } from 'vite-plugin-style-import'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import vueSetupExtend from 'vite-plugin-vue-setup-extend';
import { createHtmlPlugin } from 'vite-plugin-html';

import eslint from 'vite-plugin-eslint' // 新增

// import Markdown from 'vite-plugin-md'
// import prism from 'markdown-it-prism'
// import anchor from 'markdown-it-anchor'
// import { code, link } from 'md-powerpack'

const root = process.cwd()

export default defineConfig((data: any) => {
  let { command, mode } = data
  let env: any = null
  const isBuild = command === 'build'

  if (!isBuild) {
    env = loadEnv((process.argv[3] === '--mode' ? process.argv[4] : process.argv[3]), root)
  } else {
    env = loadEnv(mode, root)
  }
  return {
    plugins: [
      createHtmlPlugin({
        template: 'index.html',
        inject: {
          data: {
            title: env.VITE_APP_TITLE, 
            BASE_URL:'./src/assets/favicon.ico',
            // injectScript: `<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>`, // 注入脚本标签
          },
        },
      }),
      vue({
        include: [/\.vue$/, /\.md$/] // <--
      }),
      
      eslint(),

      vueSetupExtend(),

      // Markdown({
      //   markdownItOptions: {
      //     html: true,
      //     linkify: true,
      //     typographer: true
      //   },
      //   markdownItSetup(md) {
      //     // add anchor links to your H[x] tags
      //     md.use(anchor)
      //     // add code syntax highlighting with Prism
      //     md.use(prism)
      //   },
      //   builders: [link(), code()]
      // }),
      AutoImport({
        resolvers: [ElementPlusResolver()]
      }),
      Components({
        resolvers: [ElementPlusResolver()]
      }),
      createStyleImportPlugin({
        resolves: [ElementPlusResolve()],
        libs: [
          {
            libraryName: 'element-plus',
            esModule: true,
            resolveStyle: (name: string) => {
              return `element-plus/theme-chalk/${name}.css`
            }
          }
        ]
      })
    ],
    transpileDependencies: true,
    //解决“vite use `--host` to expose”
    base: './', //不加打包后白屏
    server: {
      proxy: {
        '/api': { //这里的api可以理解为后端接口里面的前缀，就是所有的地址前面都是http://aaa.com/api/xxx/xxx
          // target: 'http://jiangxi.kadadx.com/',//请求的目标地址
          // target: 'http://192.168.1.200:8081/',/请求的目标地址
          target: 'https://www.hnszwhg.cn/',//请求的目标地址
          changeOrigin: true,//是否允许跨域
          rewrite: (path) => path.replace(/^\/api/, '')
          // pathRewrite: {
          //   '^/api': '' // 重写路径（表示请求的地址没有api前缀的时候）
          // }
        }
      },
      hmr: {
        overlay: false
      },
      host: '0.0.0.0', // can be overwritten by process.env.HOST
    },
    resolve: {
      //别名配置，引用src路径下的东西可以通过@如：import Layout from '@/layout/index.vue'
      alias: [
        {
          find: '@',
          replacement: resolve(__dirname, 'src'),
        },
      ],
    },
    css: {
      preprocessorOptions: {
        scss: {
          // additionalData: '@import "@/assets/css/minxins/index.scss";'
        }
      }
    },
    build: {
      outDir: env.VITE_OUT_DIR || 'dist',
      minify: true, // 表示打包后的文件内容进行压缩，默认是true
      rollupOptions: {
        output: {
          manualChunks(id) {
            console.log(id);
            
            if (id.includes('node_modules')) {
              // 将 node_modules 中的依赖单独打包
              return 'vendor';
            }
            if (id.includes('element-plus')) {
              return 'element-plus'
            }
            // 文件命名规则
            },
            // chunkFileNames: 'js/[name]-[hash].js',
            // entryFileNames: 'js/[name]-[hash].js',
            // assetFileNames: 'assets/[name]-[hash][extname]'
        }
      },
    }
  }
});

